﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GenericValidation;
using GenericValidation.Sample.Resources;
using GenericValidation.Validators;
using GenericValidation.Sample.Model;

namespace GenericValidation.Sample.Validation
{
    public class EmployeeValidation : Validation<Employee>
    {
        public EmployeeValidation(IValidationFactory factory)
        {
            //inheriting from Validation<User>
            Inherit<User>(factory);

            //Enable Localization/Globalization: Employee member names: Age, CompanyName (to be further used in the member validation messages)
            LocalizeMembers(Members.ResourceManager);

            //Enable Localization/Globalization: setup resource manager(s) for the validation messages
            LocalizeMessages(Messages.ResourceManager);

            Member(p => p.CompanyName)
                .IsRequired()
                .When(p => p.Salary > 0)
                .MessageClear()
                .Message("InvalidSalary", p => p.Salary)
                .Always();

            Member(p => p.FullName)
                .IsRequired()
                .OnInsert()
                .OnUpdate()
                .Tag("CheckFullName");

            Member(p => p.Age).IsRequired().Always();

            IsValid(p => p.Age < 100)
                .Always()
                .Message("No one lives so much!...{0} years", p => p.Age);

            Member(p => p.DOB)
                .IsGreaterThan(DateTime.Now)
                .Always();

            Member(p => p.CompanyName)
                .IsLengthLowerThan(10)
                .Always();

            IsValid(p => this.IsEmployeeValid(p))
                .Always()
                .Message("Employee must have a salary!");
        }

        public bool IsEmployeeValid(Employee employee)
        {
            return string.IsNullOrEmpty(employee.CompanyName) == false && employee.Salary > 0;
        }
    }
}
